// api.js
//转码
function ascii2native(strAscii) {
  var output = "";
  var tempstr = "", templength = 0
  // console.log(strAscii);
  var posTo = strAscii.indexOf("\\u")
  if (posTo >= 0) {
    while (posTo >= 0) {
      //去除u
      // console.log("进来了")
      strAscii = strAscii.substr(2)
      // console.log("strAscii:"+strAscii)
      //后去第二个u
      posTo = strAscii.indexOf("\\u")
      if (posTo >= 0) {
        tempstr = strAscii.substr(0, posTo)
        // console.log("tempstr:"+tempstr)
        output += String.fromCodePoint(parseInt(tempstr, 16))
        // console.log("output:"+output)
        templength = tempstr.length
        strAscii = strAscii.substr(templength)
        // console.log("strAscii:"+strAscii)
      } else {
        output += String.fromCodePoint(parseInt(strAscii, 16))
        // console.log(output)
      }
    }
  } else {
    output = "none";
  }
  // console.log(output);
  //output += strAscii.substr(posFrom);
  return output;
}
//json转字符串
function stringToJson(data) {
  return JSON.parse(data);
}
//字符串转json
function jsonToString(data) {
  return JSON.stringify(data);
}
//map转换为json
function mapToJson(map) {
  return JSON.stringify(strMapToObj(map));
}
//json转换为map
function jsonToMap(jsonStr) {
  return objToStrMap(JSON.parse(jsonStr));
}
//map转化为对象（map所有键都是字符串，可以将其转换为对象）
function strMapToObj(strMap) {
  let obj = Object.create(null);
  for (let [k, v] of strMap) {
    obj[k] = v;
  }
  return obj;
}
//对象转换为Map
function objToStrMap(obj) {
  let strMap = new Map();
  for (let k of Object.keys(obj)) {
    strMap.set(k, obj[k]);
  }
  return strMap;
}
module.exports = {
  ascii2native: ascii2native,
  stringToJson: stringToJson,
  jsonToString: jsonToString,
  mapToJson: mapToJson,
  jsonToMap: jsonToMap,
  strMapToObj: strMapToObj,
  objToStrMap: objToStrMap,
}